//  导入官方实现、Redux 开发工具包
import { applyMiddleware, combineReducers, legacy_createStore as createStore } from 'redux'

// 导入持久化所需要的插件
import { persistReducer, persistStore } from 'redux-persist'

// 导入本地存储插件，可选storage，cookie，session等
import storage from 'redux-persist/lib/storage'

// redux官方一个异步中间件
import reduxThunk from 'redux-thunk'

// 导入子模块reducers
import theme from './modules/theme/reducer'

// 常量
import { PERSIT_CONFIG_MU } from './constant'

// 合并Reducer
// const reducer = combineReducers({
//   theme
// })

const reducer: any = theme


// persitConfig configuration information
const persitConfig = {
  key: PERSIT_CONFIG_MU,
  storage: storage
  // whitelist: [], // 需要持久化保存的模块，默认保存所有模块（语义：白名单）
  // blacklist: [], // 不需要持久化保存的模块，默认不保存任何模块（语义：黑名单）
}

// 创建配置持久信息
const persist_reducers = persistReducer(persitConfig, reducer)

// 解决 React 开发中同一功能支持多个分派和异步操作的问题
let store = createStore(persist_reducers, applyMiddleware(reduxThunk))

// 创建持久化后的store
const persistor = persistStore(store)

// 清理本地存储
const clearPersistor = () => {
  localStorage.removeItem(`persist:${PERSIT_CONFIG_MU}`)
}
// 导出store和持久化后的store，及清理函数
export { clearPersistor, persistor, store }